import logging
import platform

from motor.motor_asyncio import AsyncIOMotorClient

from re_common.baselibrary.utils.baseip import BaseIp

from apps.sql_app.database import sets


class MongoDBClient:
    # dbjsonhtml: AsyncIOMotorClient = None  # 集群连接对象
    # dbjsonlatest: AsyncIOMotorClient = None  # 集群连接对象
    dbjsonhtmlsdb: AsyncIOMotorClient = None  # 集群连接对象
    dbjsonhtmllatestsdb: AsyncIOMotorClient = None  # 集群连接对象
    dbjsonoversealatestsdb: AsyncIOMotorClient = None  # 集群连接对象
    # test: AsyncIOMotorClient = None  # 集群连接对象
    sdb_jsonhtml_conn = None


class DataBase:
    client: AsyncIOMotorClient = None
    htmljson = None


async def connect_to_mongo(mongodb_conn_str: str, db_name: str):
    logging.info("connecting to mongo...")
    db = DataBase()
    db.client = AsyncIOMotorClient(mongodb_conn_str,
                                   maxPoolSize=10,
                                   minPoolSize=10)
    # get a collection
    # Format db.<database_name>.<collection_name>
    setattr(DataBase, db_name, db.client[db_name])
    logging.info("connected to {}".format(db_name))
    return db


async def close_mongo_connection(db):
    logging.info("closing connection...")
    db.client.close()
    logging.info("closed connection")


class CollMongo(object):
    # 通过集合明获取数据库名的字典
    col_db_dicts = {
        "justtest": "crawler_logs_new"
    }

    @classmethod
    def get_table_conn(cls, table):
        return eval("Coll.get_{}".format(table))()

    @classmethod
    def get_step_info(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.step_info

    @classmethod
    def get_cnkiimage_latest_hdd(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest_hdd.cnkiimage_latest_hdd

    @classmethod
    def get_cnkiimage(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.jsonhtml_hdd.cnkiimage

    @classmethod
    def get_cnkiimage_html(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.cnkiimage_html.cnkiimage_html

    @classmethod
    def get_ref_deprecated(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.ref_deprecated

    @classmethod
    def get_wanfang_ref(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.wanfang_ref

    @classmethod
    def get_paper(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.paper

    # 用作错误捕获测试表1
    @classmethod
    def get_errs(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.err_test

    # 用作错误捕获测试表2
    @classmethod
    def get_errs_part2(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.justtest

    @classmethod
    def get_cnkiimage_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.cnkiimage_latest

    @classmethod
    def get_journal_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.journal_latest

    @classmethod
    def get_cnkipatent_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.cnkipatent_latest

    @classmethod
    def get_journal_ref_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.journal_ref_latest

    @classmethod
    def get_journal_fulltext_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.journal_fulltext_latest

    @classmethod
    def get_policy_fulltext_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.policy_fulltext_latest

    @classmethod
    def get_oversea_meta_latest(cls):
        return MongoDBClient.dbjsonoversealatestsdb.client.oversea_latest.oversea_meta_latest

    @classmethod
    def get_oversea_ref_latest(cls):
        return MongoDBClient.dbjsonoversealatestsdb.client.oversea_latest.oversea_ref_latest


    @classmethod
    def get_policy_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.policy_latest

    @classmethod
    def get_nssd_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.nssd_latest

    @classmethod
    def get_other_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.other_latest

    @classmethod
    def get_other_ref_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.other_ref_latest

    @classmethod
    def get_validate_error_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.validate_error_latest

    @classmethod
    def get_etl_suc_logs(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.etl_suc_logs

    @classmethod
    def get_a_paper_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.a_paper_latest

    @classmethod
    def get_paper_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.paper_latest

    @classmethod
    def get_paper_fulltext_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.paper_fulltext_latest

    @classmethod
    def get_other(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.other

    @classmethod
    def get_cnki_article_detail_new(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.cnki_article_detail_new

    @classmethod
    def get_except_client(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.client_except

    @classmethod
    def get_request_api_fail(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.client_api_fail

    @classmethod
    def get_post_user_err(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.user_find_err

    @classmethod
    def get_server_exception(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.server_exception

    @classmethod
    def get_ftp_patent_raw(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.patent.ftp_patent_raw

    @classmethod
    def get_ftp_patent_raw_test(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.patent.ftp_patent_raw_test

    @classmethod
    def get_ftp_patent_his(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.patent.ftp_patent_his

    @classmethod
    def get_journal_article(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.journal_article

    @classmethod
    def get_func_time(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.func_time

    @classmethod
    def get_request_time(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.request_time

    @classmethod
    def get_client_api_fail(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.crawler_logs_new.client_api_fail

    @classmethod
    def get_html_other_lob_metadata(cls):
        return MongoDBClient.dbjsonhtmlsdb.client.html_other.html_other_lob_metadata

    @classmethod
    def get_chaoxing_article_zt(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.html_other.chaoxing_article_zt

    @classmethod
    def get_cnkictbdimage_html(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.cnkiimage_html.cnkictbdimage_html

    @classmethod
    def get_cnkictbdimage_latest_hdd(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.house_latest.cnkictbdimage_latest

    @classmethod
    def get_cqvipqyreport_latest_new(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.cqvipqyreport_latest_new

    @classmethod
    def get_cqvipqyreport_fulltext_latest_new(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.cqvipqyreport_fulltext_latest_new

    @classmethod
    def get_medjournalsyiigle_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.medjournalsyiigle_latest
    @classmethod
    def get_medjournalsyiigle_fulltext_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.medjournalsyiigle_fulltext_latest
    @classmethod
    def get_medjournalsyiigle_ref_latest(cls):
        return MongoDBClient.dbjsonhtmllatestsdb.client.htmljsonlatest.medjournalsyiigle_ref_latest

Coll = CollMongo
